/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MailGUI.java
 *
 * Created on 02.04.2012, 10:37:39
 */
package GUI;

import Mailer.Mail;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author dgreipl
 */
public class MailGUI extends javax.swing.JFrame {

    /** Creates new form MailGUI */
    public MailGUI() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelRecipient = new javax.swing.JLabel();
        jTextFieldRecipient = new javax.swing.JTextField();
        jLabelMessage = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaMessage = new javax.swing.JTextArea();
        jButtonSend = new javax.swing.JButton();
        jLabelStatus = new javax.swing.JLabel();
        jTextFieldSender = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jCheckBoxSendCopyToSender = new javax.swing.JCheckBox();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabelRecipient.setText("Empfänger");
        jLabelRecipient.setToolTipText("Hier den Empfänger eingeben!");

        jTextFieldRecipient.setBackground(new java.awt.Color(255, 255, 51));
        jTextFieldRecipient.setText("kompwi@gmail.com");
        jTextFieldRecipient.setToolTipText("Ja, genau hier!!");

        jLabelMessage.setText("Nachricht:");

        jTextAreaMessage.setColumns(20);
        jTextAreaMessage.setRows(5);
        jScrollPane1.setViewportView(jTextAreaMessage);

        jButtonSend.setText("Sende");
        jButtonSend.setToolTipText("Klicken, um Mail zu senden");
        jButtonSend.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jButtonSendMouseEntered(evt);
            }
        });
        jButtonSend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSendActionPerformed(evt);
            }
        });

        jLabelStatus.setToolTipText("Hier werden Nachrichten angezeigt.");

        jTextFieldSender.setText("dg@fh-landshut.de");

        jLabel1.setText("Sender");

        jCheckBoxSendCopyToSender.setText("Kopie an Sender");
        jCheckBoxSendCopyToSender.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBoxSendCopyToSenderActionPerformed(evt);
            }
        });

        jMenu1.setText("File");

        jMenuItem1.setText("Open");
        jMenu1.add(jMenuItem1);

        jMenuItem2.setText("Save ");
        jMenu1.add(jMenuItem2);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabelStatus, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jLabelMessage, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabelRecipient, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jCheckBoxSendCopyToSender)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jButtonSend)
                        .addComponent(jScrollPane1)
                        .addComponent(jTextFieldRecipient, javax.swing.GroupLayout.DEFAULT_SIZE, 243, Short.MAX_VALUE)
                        .addComponent(jTextFieldSender)))
                .addContainerGap(66, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldSender, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelRecipient)
                    .addComponent(jTextFieldRecipient, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelMessage)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButtonSend)
                .addGap(18, 18, 18)
                .addComponent(jCheckBoxSendCopyToSender)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                .addComponent(jLabelStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonSendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSendActionPerformed
        String sender = jTextFieldSender.getText();
        String recipient = jTextFieldRecipient.getText();
        String message = jTextAreaMessage.getText();
        String subject = "Betreff";
        jLabelStatus.setText("Sende E-Mail ...");
        try {
            Mail.postMail(message, subject, recipient, sender);
            jLabelStatus.setText("Mail erfolgreich gesendet.");
            if (jCheckBoxSendCopyToSender.isSelected()) {
                Mail.postMail(message, subject, sender, sender);
                jLabelStatus.setText("Mail und Kopie erfolgreich gesendet.");
            }
        } catch (Exception e) {
            jLabelStatus.setText("Fehler : " + e);
        }
    }//GEN-LAST:event_jButtonSendActionPerformed

    private void jButtonSendMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonSendMouseEntered
        jLabelStatus.setText("Reingefahren...");
        // TODO add your handling code here:
    }//GEN-LAST:event_jButtonSendMouseEntered

    private void jCheckBoxSendCopyToSenderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxSendCopyToSenderActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jCheckBoxSendCopyToSenderActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        String plaf = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
        try {
            UIManager.setLookAndFeel(plaf);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(MailGUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(MailGUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(MailGUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(MailGUI.class.getName()).log(Level.SEVERE, null, ex);
        }

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MailGUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonSend;
    private javax.swing.JCheckBox jCheckBoxSendCopyToSender;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabelMessage;
    private javax.swing.JLabel jLabelRecipient;
    private javax.swing.JLabel jLabelStatus;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextAreaMessage;
    private javax.swing.JTextField jTextFieldRecipient;
    private javax.swing.JTextField jTextFieldSender;
    // End of variables declaration//GEN-END:variables
    
    boolean checkEMail(String rec){
        if (!rec.contains("@")) return false;
        if (rec.contains(" ")) return false;
        // Weitere prüfungen hier ergänzen
        return true;
    }
}
